package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理相关接口:")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @PostMapping
    @ApiOperation("新增套餐接口")
    @CacheEvict(cacheNames = "setmealCaChe" ,key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐接口:{}",setmealDTO);
        setmealService.insert(setmealDTO);
        return Result.success();
    }

    @ApiOperation("分页查询套餐:")
    @GetMapping("/page")
    public Result<PageResult> queryPage (SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询套餐:{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.queryPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("根据套餐ids来删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCaChe" ,allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids){
        setmealService.deleteByIds(ids);
        return Result.success();
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("{id}")
    public Result<SetmealVO> getById(@PathVariable long id){
        SetmealVO setmealVO = setmealService.getByIdWithDish(id);

        return Result.success(setmealVO);
    }

    @ApiOperation("修改套餐信息")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCaChe" ,allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐信息:{}",setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售、停售
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCaChe" ,allEntries = true)
    public Result startOrNot(@PathVariable Integer status,long id){
        log.info("修改套餐起售、停售信息:{}",status == StatusConstant.ENABLE?"起售":"停售");
        setmealService.startOrNot(status,id);
        return Result.success();
    }
}
